<?php
/**
 * Created by PhpStorm.
 * User: ewen
 * Date: 2019-12-31
 * Time: 15:52
 */

namespace app\admin\controller;


use app\common\model\WebUser;

class Index extends Base
{

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        return $this->fetch();
    }

    /**
     * TODO EWEN 添加常用操作，建表，做功能
     * 控制台
     * @return mixed
     */
    public function console()
    {
        return $this->fetch();
    }

    /**
     * 标签墙
     * @return mixed
     */
    public function labels()
    {

        return $this->fetch();
    }

    /**
     * 删除记录
     */
    public function delRecords()
    {
        !IS_POST && backError(lang('illegal request'));
        $s_id = isset($_POST['ids']) ? $_POST['ids'] : '0';
        !$s_id && backError(lang('delete at least one piece of data'));
        $table = I('post.table');
        !$table && backError(lang('table name can not be empty'));

        //  查看是否有权限
        switch ($table) {
            case 'admin_role':
                $hasAuth = checkAuth(session('adminInfo')['id'], 10);
                !$hasAuth && backError(lang('permission denied'));
                break;
            case 'admin':
                $hasAuth = checkAuth(session('adminInfo')['id'], 5);
                !$hasAuth && backError(lang('permission denied'));
                break;
        }

        $res = deleteRecords($table, 'id', $s_id, 'isDel');
        $res['status'] == 1 && adminLog('后台删除管理员，时间：' . date('Y-m-d H:i:s'));
        ajaxReturn($res);
    }

    /**
     * 用户列表
     */
    public function userList()
    {
        $map = [];
        $keywords = I('keywords/s', '');//关键词
        $keywords && $map['keywords'] = $keywords;
        $this->assign('keywords', $keywords);
        $list = WebUser::getUserList($map, 25);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 编辑用户类型
     */
    public function editUserType()
    {
        $ids = I('ids/s', '');
        $this->assign('ids', $ids);

        if (IS_POST) {
            $ids = I('ids/s', '');
            $ids = formatParamString($ids);
            count($ids) <= 0 && backError(lang('select user please'));
            $userType = I('userType/d', 1);
            $res = WebUser::where(['id' => ['in', $ids]])->save([
                'userType' => $userType
            ]);
            $res ? backSuccess() : backError();
        }
        return $this->fetch();
    }
}